﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Namespace Arwani
    Partial Class _Default
        Inherits System.Web.UI.Page

        Protected BannerHTML As String = ""
        Protected strBrands As String = ""
        Protected strFeaturedHTML As String = ""
        Public Sub getDivisions()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division "
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dtDivisions.DataSource = dtTable
            Me.dtDivisions.DataBind()
        End Sub
        Public Sub setPageSEOInformation()
            Dim thisURL As String = Request.Url.AbsoluteUri.Substring(Request.Url.AbsoluteUri.LastIndexOf("/") + 1)
            thisURL = thisURL.Substring(0, thisURL.IndexOf(".aspx") + 5)
            Dim keyword As New HtmlMeta
            keyword.Name = "keyword"


            Dim description As New HtmlMeta
            description.Name = "Description"


            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from AddSEO where PageName='" & thisURL.ToLower() & "'"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim iCount As Integer = 0
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("Title")) Then Page.Title = dtTable.Rows(0)("Title")
                If Not IsDBNull(dtTable.Rows(0)("keywords")) Then keyword.Content = dtTable.Rows(0)("keywords")
                If Not IsDBNull(dtTable.Rows(0)("Description")) Then description.Content = dtTable.Rows(0)("Description")
            End If

            Page.Header.Controls.Add(keyword)
            Page.Header.Controls.Add(description)
        End Sub
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Call loadHomePageBanners()
            Call loadFeaturedProjects()
            If Page.IsPostBack = True Then Exit Sub
            Call getDivisions()
            Call loadBrandImages()
            Call setPageSEOInformation()

        End Sub

        Public Function getImage(ByVal _Folder As String, ByVal Prefix As String, ByVal imgSrc As String) As String
            Dim strReturn As String = "images/main_banner.png"
            If File.Exists(Server.MapPath(_Folder & "/" & Prefix & imgSrc)) Then
                strReturn = _Folder & "/" & Prefix & imgSrc
            End If
            Return strReturn
        End Function

        Public Sub loadHomePageBanners()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from home_page_banners order by sortorder "
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim iCount As Integer = 0
            
            Dim arrDataStartalign() As String = {"right,bottom", "top,right", "left,bottom", "right,top", "center,top", "right,bottom", "center,bottom"}

            Dim arrDataZoom() As String = {"in", "out"}


            Dim arrDataTransition() As String = {"fade", "slide"}


            Dim arrDataZoomFact() As String = {"1", "1.2", "1.1", "1.3", "1.5", "1.9", "1.0"}


            Dim arrDataPanduration() As String = {"12", "14", "18", "9", "10", "17", "13"}


            Dim arrDataEndAlign() As String = {"center,top", "left,bottom", "center,center", "center,bottom", "right,top", "right,bottom", "center,right"}

            Dim DataStartalign As Integer = 0
            Dim DataZoom As Integer = 0
            Dim DataZoomFact As Integer = 0
            Dim DataTransition As Integer = 0
            Dim DataEndAlign As Integer = 0
            Dim DataPanduration As Integer = 0

            'BannerHTML = "<span class='bpoints'>"
            'For iCount = 0 To dtTable.Rows.Count - 1
            '    If iCount = 0 Then
            '        BannerHTML &= "<a onclick=""javascript:$('#banner_" & iCount & "').show('slow');"" href='javascript:' class='active'></a>"
            '    Else
            '        BannerHTML &= "<a onclick=""javascript:$('#banner_" & iCount & "').show('slow');"" href='javascript:'></a>"
            '    End If
            'Next
            'BannerHTML &= "</span>"
            For iCount = 0 To dtTable.Rows.Count - 1
                'If iCount = 0 Then
                '    BannerHTML &= "<img id='banner_" & iCount & "' src='" & getImage("HomeBanners", "Thumb_", dtTable.Rows(iCount)("banner_image")) & "' width='577' height='395'  />"
                'Else
                'BannerHTML &= "<a href='#'> <img id='banner_" & iCount & "' src='" & getImage("HomeBanners", "Thumb_", dtTable.Rows(iCount)("banner_image")) & "' width='577' height='395'   /></a>"
                BannerHTML &= "<li data-transition='" & arrDataTransition(DataTransition) & "' data-startalign='" & arrDataStartalign(DataStartalign) & "' data-zoom='" & arrDataZoom(DataZoom) & "' data-zoomfact='" & arrDataZoomFact(DataZoomFact) & "' data-endalign='" & arrDataEndAlign(DataEndAlign) & "' data-panduration='" & arrDataPanduration(DataPanduration) & "' data-colortransition='4'><img alt='' src='" & getImage("HomeBanners", "", dtTable.Rows(iCount)("banner_image")) & "' />"
                BannerHTML &= "<div class='creative_layer'>"
                BannerHTML &= "<div class='cp-left faderight'>"
                BannerHTML &= " <p class='cp-title'>&nbsp; </p>"
                BannerHTML &= "<div class='clear'> </div>"
                BannerHTML &= "</div>"
                BannerHTML &= "</div>"
                BannerHTML &= "</li>"
                'End If

                DataStartalign = DataStartalign + 1
                If DataStartalign = 7 Then
                    DataStartalign = 0
                End If
                DataZoom = DataZoom + 1
                If DataZoom = 2 Then
                    DataZoom = 0
                End If
                DataZoomFact = DataZoomFact + 1
                If DataZoomFact = 7 Then
                    DataZoomFact = 0
                End If

                DataTransition = DataTransition + 1
                If DataTransition = 2 Then
                    DataTransition = 0
                End If
                DataEndAlign = DataEndAlign + 1
                If DataEndAlign = 7 Then
                    DataEndAlign = 0
                End If

                DataPanduration = DataPanduration + 1
                If DataPanduration = 7 Then
                    DataPanduration = 0
                End If

            Next
        End Sub
        Public Sub loadBrandImages()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from brand order by sortorder"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

            Dim iCount As Integer = 0
            For iCount = 0 To dtTable.Rows.Count - 1
                Dim _link As String = ""
                Dim displayOption As Integer = getDisplaOption(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 1 Then _link = "_Product_Signle.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 2 Then _link = "_Product_Double.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 4 Then _link = "_Product_4.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                If Not IsDBNull(dtTable.Rows(iCount)("brandlogo")) Then strBrands &= "<a href='" & _link & "'><img border=13 style='border-color:white' width='180' height='60' src='" & getImage("Brands", "Thumb_", dtTable.Rows(iCount)("brandlogo")) & "' alt='pic' /></a>"
            Next
        End Sub

        Public Function getBrandDivision(ByVal BrandID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division_brands where brandid=" & CInt(BrandID)
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Return CInt(dtTable.Rows(0)("divisionid").ToString())
            End If
            Return 1
        End Function

        Public Sub loadFeaturedProjects()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from product  where isfeatured=1 "
            'select * from product_images where productid in ( select productid from product  where isfeatured=1) 
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim iCount As Integer = 0
            For iCount = 0 To dtTable.Rows.Count - 1
                Dim _link As String = ""
                Dim displayOption As Integer = getDisplaOption(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 1 Then _link = "Product_Signle.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 2 Then _link = "Product_Double.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                If displayOption = 4 Then _link = "Product_4.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & getBrandDivision(CInt(dtTable.Rows(iCount)("brandid")))
                strFeaturedHTML &= "<div><a href='" & _link & "'><div class='thumb'><img src='" & getImage("Products", "Thumb_Four_", getProductImage(dtTable.Rows(iCount)("productid"))) & "' width='116' height='114'  /></div>"
                strFeaturedHTML &= "<div class='f_desc'><h3>" & dtTable.Rows(iCount)("prodname") & "</h3><p>" & dtTable.Rows(iCount)("prodname") & "</p></div></a></div>"
            Next
        End Sub

        Public Function getProductImage(ByVal productID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from product_images where productid=" & CInt(productID)
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Return dtTable.Rows(0)("productimage")
            End If
            Return ""
        End Function

        Public Function getDisplaOption(ByVal BrandID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from brand where brandid=" & CInt(BrandID)
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

            If dtTable.Rows.Count > 0 Then
                If dtTable.Rows(0)("displayoption").ToString() <> "" Then
                    Return CInt(dtTable.Rows(0)("displayoption").ToString())
                End If

            End If
            Return 1
        End Function
        Public Shared Function GenerateURL(ByVal Title As Object, ByVal strId As Object) As String
            Dim strTitle As String = Title.ToString()

            '#Region "Generate SEO Friendly URL based on Title"
            'Trim Start and End Spaces.
            strTitle = strTitle.Trim()

            'Trim "-" Hyphen
            strTitle = strTitle.Trim("-"c)

            strTitle = strTitle.ToLower()
            Dim chars As Char() = "$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray()
            strTitle = strTitle.Replace("c#", "C-Sharp")
            strTitle = strTitle.Replace("vb.net", "VB-Net")
            strTitle = strTitle.Replace("asp.net", "Asp-Net")

            'Replace . with - hyphen
            strTitle = strTitle.Replace(".", "-")

            'Replace Special-Characters
            For i As Integer = 0 To chars.Length - 1
                Dim strChar As String = chars.GetValue(i).ToString()
                If strTitle.Contains(strChar) Then
                    strTitle = strTitle.Replace(strChar, String.Empty)
                End If
            Next

            'Replace all spaces with one "-" hyphen
            ' strTitle = strTitle.Replace(" ", "");

            'Replace multiple "-" hyphen with single "-" hyphen.
            strTitle = strTitle.Replace("--", "-")
            strTitle = strTitle.Replace("---", "-")
            strTitle = strTitle.Replace("----", "-")
            strTitle = strTitle.Replace("-----", "-")
            strTitle = strTitle.Replace("----", "-")
            strTitle = strTitle.Replace("---", "-")
            strTitle = strTitle.Replace("--", "-")

            'Run the code again...
            'Trim Start and End Spaces.
            strTitle = strTitle.Trim()

            'Trim "-" Hyphen
            strTitle = strTitle.Trim("-"c)
            '#End Region

            'Append ID at the end of SEO Friendly URL
            strTitle = "~/" & strTitle & "-" & Convert.ToString(strId) & ".aspx"

            Return strTitle
        End Function
        Protected Sub dtDivisions_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtDivisions.ItemCommand
            If e.CommandName = "View" Then
                Response.Redirect("Brands.aspx?divid=" & CInt(e.CommandArgument))
            End If
        End Sub

        
      
    End Class
End Namespace